﻿using Ongoal.Quotation.Service.Model.basemodel;
using Ongoal.Quotation.Service.ViewModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Ongoal.Quotation.Service.Common
{
    /// <summary>
    /// 连接字符串类
    /// </summary>
    public class SqlsugarCommon
    {
        public SqlSugarClient GetTryDb(int dbId, SqlSugarClient db)
        {
            var database = db.Queryable<T_DataSource>().Where(z => z.ID == dbId).Select(z => new DataSourceView
            {
                SourceType = z.SourceType,
                ConnectString = z.ConnectString,
                BackupType = z.BackupType,
                BaseID = z.BaseID,
                CreateTime = z.CreateTime,
                CreatorID = z.CreatorID,
                DatabaseType = (SqlSugar.DbType)Convert.ToInt32(z.DatabaseType),
                ID = z.ID,
                ModifyID = z.ModifyID,
                ModifyTime = z.ModifyTime,
                Name = z.Name,
                Remark = z.Remark
            }).First();
            return GetTryDbClass.GetTryDb(database);
        }
    }
    public class GetTryDbClass
    {
        private static SqlSugarClient instance = null;
        private GetTryDbClass()
        {


        }
        public static SqlSugarClient GetTryDb(DataSourceView model)
        {

            try
            {
                SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = model.ConnectString,
                    DbType = model.DatabaseType,
                    IsAutoCloseConnection = true//自动释放
                });
                instance = db;
                return instance;
            }
            catch
            {

                throw new Exception(model.ConnectString + " " + model.DatabaseType + "无法连接到数据库，请认真检查DbType和连接字符串");
            }
        }

    }
}
